﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class AnnouncementTest : GenericModuleTest
    {
        protected override string moduleName
        {
            get
            {
                return "Announcement Test";
            }
        }
        protected string updateMethod;
        protected string addMethod;

        public AnnouncementTest(string accessToken, string cid)
            : base(accessToken)
        {
            this.viewAllMethod = "viewAllAnnouncements";
            this.viewSingleItemMethod = "viewAnnouncement";
            this.addMethod = "addAnnouncement";
            this.updateMethod = "updateAnnouncement";
            this.deleteMethod = "deleteAnnouncement";
            this.uploadMethod = "uploadInAnnouncement";
            this.cid = cid;
        }

        protected override string genericQueryStringGenerate(int validItemID)
        {
            string queryString = new Dictionary<string, string>() { { "cid", this.cid }, { "accesstoken", this.accesstoken }, { "itemid", validItemID.ToString() } }.buildQueryString();

            return queryString;
        }
        protected override Dictionary<string, object> genericPostDataSetGenerate()
        {
            Dictionary<string, object> dataSet = new Dictionary<string, object>();
            {
                dataSet.Add("body", Library.getDescription());
                dataSet.Add("title", Library.getTitle());
                dataSet.Add("expireTime", DateTime.Now.AddYears(1).convertToUnixtime().ToString());
            }
            return dataSet;
        }

        protected List<LogEntry> addOrUpdate(bool doAddOperation)
        {
            List<LogEntry> entries = new List<LogEntry>();
            string methodUrl, queryString;

            LogEntry t1 = new LogEntry(this.moduleName, true);
            if (doAddOperation == true)
            {
                methodUrl = this.addMethod;
                queryString = new Dictionary<string, string>() { { "accessToken", this.accesstoken }, { "cid", this.cid } }.buildQueryString();
            }
            else
            {
                methodUrl = this.updateMethod;
                queryString = this.genericQueryStringGenerate(this.validItemIDs.randomElement());
            }
            HTTP.execute(HTTP.RequestType.POST, this.rootUrl, methodUrl, queryString, this.genericPostDataSetGenerate(), ref t1);
            t1.verdict &= API_VERDICT.checkAddOrUpdateOperation(t1.receivedData, doAddOperation);
            entries.Add(t1);
            return entries;
        }

        public List<LogEntry> start(string localFile)
        {
            Console.WriteLine("Announcement API call begin\n============");
            List<LogEntry> entries = new List<LogEntry>();

            entries.AddRange(this.addOrUpdate(true));
            entries.AddRange(this.viewAll());
            entries.AddRange(this.viewSingle());
            entries.AddRange(this.addOrUpdate(false));
            entries.AddRange(this.uploadFile(localFile, 4, this.uploadMethod));
            entries.AddRange(this.delete());

            return entries;
        }
    }
}
